package com.msg2phone.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkUtil {
	/**
	 * 判断当前网络状态是否可用
	 * @param context
	 * @return
	 */
	public static boolean getNetworkState(Context context){
		ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = connectivityManager.getActiveNetworkInfo(); 

		if(info != null)
		{
			return true;
		}

		return false;
	}

	/**
	 * 判断网络类型
	 * @param context
	 * @return
	 */
	public static NetType getNetType(Context context){
		ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if(connectivityManager == null) return null; 

		NetworkInfo info = connectivityManager.getActiveNetworkInfo();
		if(info == null) return null;

		String type = info.getTypeName();

		if (type.equalsIgnoreCase("WIFI")) {
			// WIFI方式
			return null;
		} else if(type.equalsIgnoreCase("MOBILE")) {
			// GPRS方式
			String proxyHost = android.net.Proxy.getDefaultHost();
	        if (proxyHost != null && !proxyHost.equals("")) {
	        	// WAP方式
	        	NetType netType = new NetType();
	            netType.setProxy(proxyHost);
	        	netType.setPort(android.net.Proxy.getDefaultPort());
	        	netType.setWap(true);
	        	return netType;
	        }
		}
		return null;
	}
}
